package com.example.zg.zg_312_ordering_new.activity;

import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zg.zg_312_ordering_new.R;
import com.example.zg.zg_312_ordering_new.adapter.ShopAdapter;
import com.example.zg.zg_312_ordering_new.beans.Shop;
import com.example.zg.zg_312_ordering_new.fragments.CollectFragment;
import com.example.zg.zg_312_ordering_new.fragments.HomeFragment;
import com.example.zg.zg_312_ordering_new.fragments.MeFragment;
import com.example.zg.zg_312_ordering_new.fragments.SearchFragment;

import java.util.List;

public class MainActivity extends FragmentActivity implements View.OnClickListener {
    private LinearLayout mTabshouye;
    private LinearLayout mTabcollect;
    private LinearLayout mTabsearch;
    private LinearLayout mTabmy;

    private ImageButton mlmgshouye;
    private ImageButton mlmgcollect;
    private ImageButton mlmgsearch;
    private ImageButton mlmgmy;

    private Fragment mTab01;
    private Fragment mTab02;
    private Fragment mTab03;
    private Fragment mTab04;

    private TextView shouye;
    private TextView collect;
    private TextView search;
    private TextView my;

    private List mDataList;
    private RecyclerView recyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        initViews();//初始化控件
        initData();//初始化事件
        initEvent();
        setSelect(0);

        ShopAdapter mAdapter=new ShopAdapter(this,mDataList);
        mAdapter.setOnItemClickListener(new ShopAdapter.OnRecyclerViewItemClickListener(){
            @Override
            public void onItemClick(View view,Object data){
                Shop shop=(Shop)data;
                Toast.makeText(MainActivity.this,shop.getShopname(),Toast.LENGTH_LONG).show();
            }
        });

//        recyclerView.setAdapter(mAdapter);
    }
    private void initEvent(){
        mTabshouye.setOnClickListener(this);
        mTabcollect.setOnClickListener(this);
        mTabsearch.setOnClickListener(this);
        mTabmy.setOnClickListener(this);
    }

    private void initViews() {
        mTabshouye=(LinearLayout)findViewById(R.id.id_tab_shouye);
        mTabcollect=(LinearLayout)findViewById(R.id.id_tab_collect);
        mTabsearch=(LinearLayout)findViewById(R.id.id_tab_search);
        mTabmy=(LinearLayout)findViewById(R.id.id_tab_my);

        mlmgshouye=(ImageButton)findViewById(R.id.id_tab_shouye_img);
        mlmgcollect=(ImageButton)findViewById(R.id.id_tab_collect_img);
        mlmgsearch=(ImageButton)findViewById(R.id.id_tab_search_img);
        mlmgmy=(ImageButton)findViewById(R.id.id_tab_my_img);

        shouye=(TextView)findViewById(R.id.shouye);
        collect=(TextView)findViewById(R.id.collect);
        search=(TextView)findViewById(R.id.search);
        my=(TextView)findViewById(R.id.my);

    }
    private void setSelect(int i) {
        FragmentManager fm=getSupportFragmentManager();
        FragmentTransaction transaction=fm.beginTransaction();
        hideFragment(transaction);
        switch (i){
            case 0:
                if (mTab01==null){
                    mTab01=new HomeFragment();
                    transaction.add(R.id.id_content,mTab01);
                }else {
                    transaction.show(mTab01);
                }
                mlmgshouye.setImageResource(R.drawable.ic_shop);
                shouye.setTextColor(Color.parseColor("#2222ff"));
                break;
            case 1:
                if (mTab02==null){
                    mTab02=new CollectFragment();
                    transaction.add(R.id.id_content,mTab02);
                }else {
                    transaction.show(mTab02);
                }
                mlmgcollect.setImageResource(R.drawable.ic_collection);
                collect.setTextColor(Color.parseColor("#2222ff"));
                break;
            case 2:
                if (mTab03==null){
                    mTab03=new SearchFragment();
                    transaction.add(R.id.id_content,mTab03);
                }else {
                    transaction.show(mTab03);
                }
                mlmgsearch.setImageResource(R.drawable.ic_search);
                search.setTextColor(Color.parseColor("#2222ff"));
                break;
            case 3:
                if (mTab04==null){
                    mTab04=new MeFragment();
                    transaction.add(R.id.id_content,mTab04);
                }else {
                    transaction.show(mTab04);
                }
                mlmgmy.setImageResource(R.drawable.ic_user);
                my.setTextColor(Color.parseColor("#2222ff"));
                break;
        }
        transaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction) {
        if (mTab01!=null){
            transaction.hide(mTab01);
            shouye.setTextColor(Color.parseColor("#8a8a8a"));
        }
        if (mTab02!=null){
            transaction.hide(mTab02);
            collect.setTextColor(Color.parseColor("#8a8a8a"));
        }
        if (mTab03!=null){
            transaction.hide(mTab03);
            search.setTextColor(Color.parseColor("#8a8a8a"));
        }
        if (mTab04!=null){
            transaction.hide(mTab04);
            my.setTextColor(Color.parseColor("#8a8a8a"));
        }
    }

    @Override
    public void onClick(View v) {
        reseImgs();
        switch (v.getId()){
            case R.id.id_tab_shouye:
                setSelect(0);
                break;
            case R.id.id_tab_collect:
                setSelect(1);
                break;
            case R.id.id_tab_search:
                setSelect(2);
                break;
            case R.id.id_tab_my:
                setSelect(3);
                break;
            default:
                break;
        }

    }

    @Override
    protected void onResume(){
        super.onResume();
    }

    private void reseImgs() {
        mlmgshouye.setImageResource(R.drawable.ic_shop);
        mlmgcollect.setImageResource(R.drawable.ic_collection);
        mlmgsearch.setImageResource(R.drawable.ic_search);
        mlmgmy.setImageResource(R.drawable.ic_user);

    }

    private void initData(){

    }
}
